﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PING工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        string host;
        Thread t;
        List<String> LIST1 = new List<string>();
        List<String> LIST2 = new List<string>();

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            host = textBox3.Text;
        }

        private void pingTask1(object i)
        {
            Ping p1 = new Ping();
            PingReply reply;
            try
            {
                reply = p1.Send(host + i.ToString());   //发送主机名或IP地址
                if (reply.Status == IPStatus.Success)
                {
                    textBox1.AppendText(host + i.ToString() + "\r\n");
                    LIST1.Add(host + i.ToString());
                }
            }
            catch
            { }
        }

        private void pingTask2(object i)
        {
            Ping p1 = new Ping();
            PingReply reply;
            try
            {
                reply = p1.Send(host + i.ToString());   //发送主机名或IP地址
                if (reply.Status == IPStatus.Success)
                {
                    textBox2.AppendText(host + i.ToString() + "\r\n");
                    LIST2.Add(host + i.ToString());
                }
            }
            catch
            { }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            t.Abort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LIST1.Clear();
            textBox1.Clear();
            for (int i = 0; i < 255; i++)
            {
                t = new Thread(pingTask1);
                t.Start(i);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LIST2.Clear();
            textBox2.Clear();
            for (int i = 0; i < 255; i++)
            {
                t = new Thread(pingTask2);
                t.Start(i);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            host = textBox3.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox4.Clear();
            List<string> ls = LIST2.Except(LIST1).ToList();
            if (ls.Count == 0)
            {
                textBox4.Text = "无";
            }
            else
            {
                foreach (string s in ls)
                {
                    textBox4.AppendText(s + "\r\n");
                }
            }
        }
    }
}
